package org.abc.fund.service.funds;

import org.abc.fund.entity.funds.FundTag;
import org.abc.fund.repository.funds.FundTagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FundTagService {

    @Autowired
    private FundTagRepository tagRepository;

    public List<FundTag> getAllTags(String category) {
        if (category != null && !category.isEmpty()) {
            return tagRepository.findByCategory(category);
        }
        return tagRepository.findAll();
    }

    public ResponseEntity<?> addTag(FundTag tag) {
        List<FundTag> existing = tagRepository.findByNameAndCategory(tag.getName(), tag.getCategory());
        if (!existing.isEmpty()) {
            return ResponseEntity.status(HttpStatus.CONFLICT).body("标签已存在，不能重复添加！");
        }
        return ResponseEntity.ok(tagRepository.save(tag));
    }

    public ResponseEntity<?> deleteTag(Long id) {
        if (!tagRepository.existsById(id)) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("标签不存在");
        }
        tagRepository.deleteById(id);
        return ResponseEntity.ok("标签已删除");
    }
}
